package com.gxa.insurance.service.impl;

import com.gxa.insurance.exception.BusinessException;
import com.gxa.insurance.exception.ExceptionCode;
import com.gxa.insurance.mapper.S4ShopMapper;
import com.gxa.insurance.pojo.S4Shop;
import com.gxa.insurance.service.S4ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author wang li sha
 * @date 2022-06-06 16:33
 */
@Service("S4ShopService")
@Transactional(rollbackFor = Throwable.class)
public class S4ShopServiceImpl implements S4ShopService {
    @Autowired
    S4ShopMapper s4ShopMapper;

    @Override
    public Integer insert(S4Shop s4Shop) throws Exception {
        Integer insert = s4ShopMapper.insert(s4Shop);
        if (insert != 1) {
            throw new BusinessException(ExceptionCode.INSERT_S4SHOP_FAILED);
        }
        return insert;
    }

    @Override
    public Integer update(S4Shop s4Shop) throws Exception {
        Integer update = s4ShopMapper.update(s4Shop);
        if (update != 1) {
            throw new BusinessException(ExceptionCode.UPDATE_S4SHOP_FAILED);
        }
        return update;
    }

    @Override
    public Integer deleteById(Integer id) throws Exception {
        Integer delete = s4ShopMapper.deleteById(id);
        if (delete != 1) {
            throw new BusinessException(ExceptionCode.DELETE_S4SHOP_FAILED);
        }
        return delete;
    }

    @Override
    public S4Shop selectById(Integer id) throws Exception {
        S4Shop s4Shop = s4ShopMapper.selectById(id);
        if (s4Shop == null) {
            throw new BusinessException(ExceptionCode.S4SHOP_NOT_FONUD);
        }
        return s4Shop;
    }

    @Override
    public List<S4Shop> selectAll() throws Exception {
        List<S4Shop> s4Shops = s4ShopMapper.selectAll();
        if (s4Shops.size() == 0) {
            throw new BusinessException(ExceptionCode.S4SHOP_IS_EMPTY);
        }
        return s4Shops;
    }


    @Override
    public List<S4Shop> selectAllByPage(Integer page, Integer size) throws Exception {
        List<S4Shop> s4Shops = s4ShopMapper.selectAllByPage((page - 1) * size, size);
        if (s4Shops.size() == 0) {
            throw new BusinessException(ExceptionCode.S4SHOP_IS_EMPTY);
        }
        return s4Shops;
    }

    @Override
    public List<S4Shop> selectLikeName(String name) throws Exception {
        List<S4Shop> s4Shops = s4ShopMapper.selectLikeName(name);
        if (s4Shops.size() == 0) {
            throw new BusinessException(ExceptionCode.S4SHOP_IS_EMPTY);
        }
        return s4Shops;
    }

    @Override
    public List<S4Shop> selectLikeNameByPage(Integer page, Integer size, String name) throws Exception {
        List<S4Shop> s4Shops = s4ShopMapper.selectLikeNameByPage((page - 1) * size, size, name);
        if (s4Shops.size() == 0) {
            throw new BusinessException(ExceptionCode.S4SHOP_IS_EMPTY);
        }
        return s4Shops;
    }
}
